package com.mine.photohandler.activity;

import android.content.Intent;
import android.graphics.Color;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mine.photohandler.R;
import com.mine.photohandler.base.BaseAppCompatActivity;
import com.mine.photohandler.util.Bimp;
import com.mine.photohandler.util.PublicWay;
import com.mine.photohandler.zoom.PhotoView;
import com.mine.photohandler.zoom.ViewPagerFixed;

import java.util.ArrayList;


/**
 * 这个是用于进行图片浏览时的界面
 *
 */
public class GalleryActivity extends BaseAppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private TextView mTv_top_title;
    private TextView mTv_right;
    private TextView mTv_ok;
    private ViewPagerFixed mVpf_preview;
    private ImageView mIv_right;

    //当前的位置
    private int current = 0;
    private ArrayList<View> mList_views = null;
    private MyPageAdapter adapter;

    @Override    protected int contentView() {
        return R.layout.plugin_activity_gallery;
    }

    @Override
    protected void initViews() {
        setStatusBar(Color.BLACK);
        mTv_top_title = (TextView) findViewById(R.id.tv_top_title);
        mTv_right = (TextView) findViewById(R.id.tv_right);
        mIv_right = (ImageView) findViewById(R.id.iv_right);
        mTv_ok = (TextView) findViewById(R.id.tv_ok);
        mVpf_preview = (ViewPagerFixed) findViewById(R.id.vpf_preview);

        mTv_right.setText("");
        mIv_right.setImageResource(R.drawable.plugin_icon_delete);
        initListViews();
        adapter = new MyPageAdapter(mList_views);
        mVpf_preview.setAdapter(adapter);
        mVpf_preview.setPageMargin(getResources().getDimensionPixelOffset(R.dimen.ui_10_dip));

        mTv_top_title.setText(getString(R.string.preview));

    }

    @Override
    protected void initEvents() {
        findViewById(R.id.rl_back).setOnClickListener(this);
        findViewById(R.id.rl_right).setOnClickListener(this);
        mTv_ok.setOnClickListener(this);

        mVpf_preview.addOnPageChangeListener(this);
    }

    private void initListViews() {
        if (mList_views == null){
            mList_views = new ArrayList<>();

        }

        for (int i = 0; i < Bimp.tempSelectBitmap.size(); i++) {
            PhotoView img = new PhotoView(this);
            img.setBackgroundColor(0xff000000);
            img.setImageBitmap(Bimp.tempSelectBitmap.get(i).getBitmap());
            img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            mList_views.add(img);
        }
    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                current = intent.getExtras().getInt("ID");
                mVpf_preview.setCurrentItem(current);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        current = position;
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mTv_ok.setText("OK" + "(" + Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.rl_back){
            startActivity(ImageFile.class,true);
        }else if (view.getId() == R.id.rl_right){
            if (mList_views.size() == 1) {
                Bimp.tempSelectBitmap.clear();
//				Bimp.max = 0;
                mTv_ok.setText("完成"+"(" + Bimp.tempSelectBitmap.size()
                        + "/"+ PublicWay.num+")");
                Intent intent = new Intent("data.broadcast.action");
                sendBroadcast(intent);
                finish();
            } else {
                Bimp.tempSelectBitmap.remove(current);
//				Bimp.max--;
                mVpf_preview.removeAllViews();
                mList_views.remove(current);
                adapter.setListViews(mList_views);
                mTv_ok.setText("完成"+"(" + Bimp.tempSelectBitmap.size()
                        + "/"+ PublicWay.num+")");
                adapter.notifyDataSetChanged();
            }
        }else if (view.getId() == R.id.tv_ok){
            overridePendingTransition(R.anim.activity_translate_in,
                    R.anim.activity_translate_out);
            finish();
        }
    }

    class MyPageAdapter extends PagerAdapter {

        private ArrayList<View> listViews;

        private int size;
        public MyPageAdapter(ArrayList<View> listViews) {
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public void setListViews(ArrayList<View> listViews) {
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public int getCount() {
            return size;
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPagerFixed) arg0).removeView(listViews.get(arg1 % size));
        }

        public void finishUpdate(View arg0) {
        }

        public Object instantiateItem(View arg0, int arg1) {
            try {
                ((ViewPagerFixed) arg0).addView(listViews.get(arg1 % size), 0);

            } catch (Exception e) {
            }
            return listViews.get(arg1 % size);
        }

        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

    }
}
